﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CmdRemoveSelectedModelView.h"
#include "GbmpUiPlatformCommandRegister.h"
#include "GbmpUiPlatformCommandIds.h"

#include "IUiDocument.h"
#include "IDocument.h"

#include "IUiDocumentViewManager.h"
#include "GcmpBuiltInCategoryUniIdentities.h"
#include "IModelView.h"
#include "IUiView.h"
#include "UiCommonDialog.h"
#include "IUserTransaction.h"
#include "CommandParameters.h"
#include "IElementBasicInformation.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
CmdRemoveSelectedModelView::CmdRemoveSelectedModelView()
    : CommandBase(ID_CMD_REMOVE_SELECTED_MODEL_VIEW, true)
{

}

CmdRemoveSelectedModelView::~CmdRemoveSelectedModelView()
{

}

gcmp::OwnerPtr<gcmp::IAction> CmdRemoveSelectedModelView::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    std::wstring modelViewType;
    auto modelViewTypeIter = cmdParams.find(L"ModelViewType");
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(modelViewTypeIter != cmdParams.end(), L"无法找到参数ModelViewType",L"GDMPLab",L"2024-03-30");
    modelViewType = modelViewTypeIter->second.AsWString();

    std::wstring modelViewName;
    auto modelViewNameIter = cmdParams.find(L"ModelViewName");
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(modelViewNameIter != cmdParams.end(), L"无法找到参数ModelViewName",L"GDMPLab",L"2024-03-30");
    modelViewName = modelViewNameIter->second.AsWString();

    IUiDocument* pUIDoc = IUiDocumentViewManager::Get()->GetCurrentUiDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUIDoc != nullptr, L"pUIDoc不能为空",L"GDMPLab",L"2024-03-30");
    IDocument *pDoc = pUIDoc->GetDbDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc != nullptr, L"pDoc不能为空",L"GDMPLab",L"2024-03-30");

    IModelView *pDeleteModelView = nullptr;
    std::vector<IElement*> modelViewArray = pDoc->GetElementsByCategory(BuiltInCategoryUniIdentities::BICU_MODEL_VIEW);
    for (IElement *pElement : modelViewArray)
    {
        IModelView *pModelView = quick_cast<IModelView>(pElement);
        if (pModelView && pModelView->GetUserDataId() == modelViewType && pModelView->GetBasicInformation()->GetName() == modelViewName)

        {
            pDeleteModelView = pModelView;
            break;
        }
    }
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDeleteModelView != nullptr, L"没找到要删除的视图",L"GDMPLab",L"2024-03-30");

    IUiView *pCurrView = IUiDocumentViewManager::Get()->GetCurrentUiView();
    if (pCurrView)
    {
        if (pCurrView->GetModelView() == pDeleteModelView)
        {
            UiCommonDialog::ShowMessageBox(GBMP_TR(L"提示"), GBMP_TR(L"不能删除当前视图"), (int)UiCommonDialog::ButtonType::OK);
        }
        else
        {
            std::wstring alarmingStr = StringUtil::FormatWString(GBMP_TR(L"是否删除%s视图?"), pDeleteModelView->GetName().c_str());
            if (UiCommonDialog::ShowMessageBox(GBMP_TR(L"警告"), alarmingStr, (int)UiCommonDialog::ButtonType::OK | (int)UiCommonDialog::ButtonType::Cancel)
                != UiCommonDialog::ButtonType::OK)
            {
                return nullptr;
            }
            std::wstring cannotDeleteExplaintion = L"";
            bool bOk = false;
            OwnerPtr<IUserTransaction> ut = IUserTransaction::Create(pUIDoc->GetDbDocument(), GBMP_TR(L"删除视图"));
            bOk = pUIDoc->GetDbDocument()->DeleteElement(pDeleteModelView->GetBasicInformation()->GetElementId(), &cannotDeleteExplaintion);
            ut->Commit();

            if (!bOk)
            {
                UiCommonDialog::ShowMessageBox(GBMP_TR(L"提示"), GBMP_TR(cannotDeleteExplaintion), (int)UiCommonDialog::ButtonType::OK);
            }

            pUIDoc->UpdateView();
        }
    }

    return nullptr;
}

REGISTER_GM_COMMAND(CmdRemoveSelectedModelView)
